﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using NHibernate;
using NHibernate.Cfg;

namespace GS.Cricket.Service.Impl.NHibernate
{

    public class NHibernateHelper<T>
    {
        private static ISessionFactory _sessionFactory;

        private static ISessionFactory SessionFactory
        {
            get
            {
                if (_sessionFactory == null)
                {
                    var configuration = new Configuration();
                    configuration.Configure();
                    configuration.AddAssembly(typeof(T).Assembly);
                    _sessionFactory = configuration.BuildSessionFactory();

                    //try
                    //{
                    //    new SchemaExport(configuration).Execute(false, true, false);
                    //}
                    //catch (Exception)
                    //{
                    //    new SchemaExport(configuration).Execute(false, true, true);
                    //    new SchemaExport(configuration).Execute(false, true, false);
                    //}

                }
                return _sessionFactory;
            }

        }

        public static ISession OpenSession()
        {
            return SessionFactory.OpenSession();
        }
    }
}
